package weaver.splittable.db.mysql;

import weaver.splittable.db.STCreateTableUtil;
import weaver.splittable.db.STDBConstants;

/**
 * 建表工具的Mysql实现
 * author: YUEYANG 2023-01-12
 */
public class STMysqlCreateTableUtil implements STCreateTableUtil {

    private static final String CREATE_DATA_CHANGE_TABLE_SQL =
            "CREATE TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" (\n" +
            " id bigint NOT NULL AUTO_INCREMENT,\n" +
            " event_type VARCHAR(32) NOT NULL,\n" +
            " table_name VARCHAR(64) NOT NULL,\n" +
            " data_id_column VARCHAR(64) NOT NULL,\n" +
            " data_id VARCHAR(32) NOT NULL,\n" +
            " synced CHAR(1) NOT NULL,\n" +
            " create_time VARCHAR(64) NOT NULL,\n" +
            " sync_time VARCHAR(64) NULL,\n" +
            " PRIMARY KEY (id)\n" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8";

    private static final String CREATE_DATA_CLEAR_TABLE_SQL =
            "CREATE TABLE "+ STDBConstants.DATA_CLEAR_TABLE_NAME +" (\n" +
            " id bigint NOT NULL AUTO_INCREMENT,\n" +
            " table_name VARCHAR(64) NOT NULL,\n" +
            " data_id_column VARCHAR(64) NOT NULL,\n" +
            " data_id VARCHAR(32) NOT NULL,\n" +
            " synced CHAR(1) NOT NULL,\n" +
            " clear_time VARCHAR(64) NOT NULL,\n" +
            " sync_time VARCHAR(64) NULL,\n" +
            " PRIMARY KEY (id)\n" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8";

    //添加阈值表创建sql
    private static final String CREATE_DATA_THRESHOLD_TABLE_SQL =
            "CREATE TABLE " + STDBConstants.DATA_THRESHOLD_TABLE_NAME + " (\n" +
                    " id bigint NOT NULL AUTO_INCREMENT,\n" +
                    " module VARCHAR(64) NOT NULL,\n" +
                    " requestid_threshold int NOT NULL,\n" +
                    " createdate_threshold VARCHAR(64) NOT NULL,\n" +
                    " updatetime VARCHAR(64) NOT NULL,\n" +
                    " PRIMARY KEY (id)\n" +
                    ")ENGINE=InnoDB DEFAULT CHARSET=utf8";

    private static final String CREATE_FULL_DATA_TABLE_SQL = "create table %s AS select * from %s";

    @Override
    public String createDataChangeTable() {
        return CREATE_DATA_CHANGE_TABLE_SQL;
    }

    @Override
    public String createDataClearTable() {
        return CREATE_DATA_CLEAR_TABLE_SQL;
    }

    @Override
    public String createDataThresholdTable() {
        return CREATE_DATA_THRESHOLD_TABLE_SQL;
    }

    @Override
    public String createFullDataTable(String tableName) {
        String fullDataTableName = tableName + "_all";
        return String.format(CREATE_FULL_DATA_TABLE_SQL, fullDataTableName, tableName);
    }

}
